package com.daumcorp.searchdata.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.daumcorp.searchdata.domain.Author;
import com.daumcorp.searchdata.domain.authorvalidate.Basic;
import com.daumcorp.searchdata.domain.authorvalidate.Extend;
import com.daumcorp.searchdata.service.AuthorService;

@Controller
@RequestMapping("/author/register")
public class AuthorRegisterController {
	@Autowired
	private AuthorService authorService;

	@RequestMapping
	public String registerAuthor(Model model) {
		model.addAttribute("basic", new Author());
		model.addAttribute("extend", new Author());

		return "authorregister";
	}

	@RequestMapping(value = "/basic", method = RequestMethod.POST)
	public String registerAuthor(@Validated({ Basic.class }) Author author,
			BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			 System.out.println("에러 있음");
		}else {
			authorService.saveAuthor(author);
		}
		return "redirect:/author/register";
	}

	@RequestMapping(value = "/extend", method = RequestMethod.POST)
	public String registerAuthorExtend(
			@Validated({ Extend.class }) Author author,
			BindingResult bindingResult) {
		if (!bindingResult.hasErrors()) {
			System.out.println("에러 있음");
		} else {
			authorService.saveAuthor(author);
		}

		return "redirect:/author/register";
	}
}